package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.Address;
import com.example.isoftservice.service.AddressService;
import com.example.isoftservice.util.Result;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @RequestMapping("/searchAddressList")
    public Result<Map<String,Object>> searchAddressList(@RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize" ,defaultValue = "5")Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setMessage("查询成功");
        result.setResult(addressService.searchAddressList(pageNo,pageSize));
        result.setCode(0);
        return  result;
    }

    @RequestMapping("/searchAddressById")
    public Result<Address> searchAddressById(Integer addressId) {
        Result<Address> result = new Result<>();
        Address address = addressService.searchAddressById(addressId);
        if(address != null) {
            result.setMessage("查询成功");
            result.setCode(0);
            result.setResult(address);
            return result;
        }else {
        result.setMessage("查询失败,城市不存在");
        result.setCode(1);
        return result;
        }

    }

    @RequestMapping(value = "/addAddress" ,method = RequestMethod.POST)
    public Result<Boolean> addAddress(@RequestBody Address address) {
        Result<Boolean> result = new Result<>();
        Boolean flag = addressService.addAddress(address);
        result.setResult(flag);
        result.setCode(0);
        result.setMessage("增加成功");
        return result;
    }

    @RequestMapping(value = "/removeAddressByIds", method = RequestMethod.POST)
    public Result<Boolean> removeAddressByIds(@RequestBody Address address) {
        Result<Boolean> result = new Result<>();
        Boolean flag = addressService.removeAddressByIds(address.getIds());
        result.setMessage("删除成功");
        result.setCode(0);
        result.setResult(flag);
        return result;
    }

    @RequestMapping(value = "/modifyAddressById",method = RequestMethod.POST)
    public  Result<Boolean> modifyAddressById(@RequestBody  Address address) {
        Result<Boolean> result = new Result<>();
        Boolean flag = addressService.modifyAddressById(address);
        if(addressService.searchAddressById(address.getAddressId()) != null) {
            result.setResult(flag);
            result.setCode(0);
            result.setMessage("更新成功");
            return result;
        } else {
            result.setCode(1);
            result.setMessage("更新失败,城市不存在");
            return result;
        }

    }
}
